#include "thread.h"

namespace concurrent {

Thread::Thread() : _running(false) {}

bool Thread::start() {
	if ( !_running && 0 == pthread_create(&_threadId, NULL, Thread::entryPoint, this) ) {
		_running = true;
		return true;
	}
	return false;	
}

bool Thread::join() {
	if ( _running && 0 == pthread_join(_threadId, NULL) ) {
		_running = false;
		return true;
	}
	return false;
}

bool Thread::sleep(unsigned int millisec) {
	if ( 0 == usleep(millisec * 1000) ) {
		return true;
	}
	return false;
}

/* static method */
void *Thread::entryPoint(void *pthis) {
	Thread *pt = (Thread*)pthis;
	pt->setup();
	pt->run();
	return pthis;
}

}
